package com.hxzy.controller;

import com.hxzy.dao.TeaCouresMapper;
import com.hxzy.dao.TeaSignInMapper;
import com.hxzy.pojo.TeaSignIn;
import com.hxzy.pojo.TeacherCoures;
import com.hxzy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TeacherController {

    @Autowired
    private TeaSignInMapper teaSignInMapper;
    @Autowired
    private TeaCouresMapper teaCouresMapper;

    @RequestMapping("teaSignIn")
    @ResponseBody
    public String teaSignIn(TeaSignIn ts, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ts.setTeaName(user.getName());
        ts.setStatus("1");
        System.out.println("TeacherController line 26= " + user);
        int insert = teaSignInMapper.insert(ts);
        if (insert > 0) {
            return "true";
        }
        return "false";
    }

    @RequestMapping("selSign/{status}")
    @ResponseBody
    public List<TeaSignIn> selSign(@PathVariable("status") Integer status) {
        Example example1 = new Example(TeaSignIn.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("status", status);
        List<TeaSignIn> teaSignIns = teaSignInMapper.selectByExample(example1);
        if (teaSignIns.size() > 0) {
            return teaSignIns;
        }
        return null;
    }

    @RequestMapping("selSignAll")
    @ResponseBody
    public List<TeaSignIn> selSignAll() {
        List<TeaSignIn> teaSignIns = teaSignInMapper.selectAll();
        if (teaSignIns.size() > 0) {
            return teaSignIns;
        }
        return null;
    }

    @RequestMapping("updSign")
    @ResponseBody
    public String updSign(TeaSignIn teaSignIn, HttpSession session) {
        User user = (User) session.getAttribute("user");
        teaSignIn.setTeaName(user.getName());
        teaSignIn.setStatus("1");

        Example example1 = new Example(TeaSignIn.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", teaSignIn.getId());
        int insert = teaSignInMapper.updateByExample(teaSignIn,example1);
        if (insert > 0) {
            return "true";
        }
        return "false";
    }

    @RequestMapping("delSign")
    @ResponseBody
    public String delSign(String id) {
        System.out.println("TeacherController line 81= " + id);
        Example example1 = new Example(TeaSignIn.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", id);
        TeaSignIn teaSignIn = teaSignInMapper.selectOneByExample(example1);
        teaSignIn.setStatus("3");
        int insert = teaSignInMapper.updateByExample(teaSignIn,example1);
        System.out.println("TeacherController line 86= " + insert);
        if (insert > 0) {
            return "true";
        }
        return "false";
    }

    //获取所有老师的总课时
    @RequestMapping("getAllteachcoure")
    @ResponseBody
    public  List<TeacherCoures> getAllteachcoure() {

        List<TeacherCoures> allteachcoure = teaCouresMapper.getAllteachcoure();
        System.out.println("allteachcoure = " + allteachcoure);

        return allteachcoure;
    }


}
